package cichlid.seprphase3.GUIInterface;

import cichlid.seprphase3.Utilities.Energy;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.image.BufferedImage;

/**
 * This interface is shown when the game is over.
 */
public class GameOverInterface extends BaseInterface implements MouseListener {

    // This animation is used to show the nuclear explosion.
    private Animation explosion;
    
    // This button is to allow the player to start a new game.
    private  PlantGUIElement playAgainButton;
    
    // This is polled by the GUIWindow - when it is set to false, the GameOverInterface is no
    // longer displayed and the menu is displayed instead.
    public Boolean block = true;
    
    // The variables passed in from the simulator, to be displayed on the GameOverScreen.
    //for 1player mode only
    private Energy powerGenerated;
    private String name;
    private boolean loading;
    
    //Two player variables
    public boolean twoPlayer;
    public boolean afterFirst;
    
    //player1
    private Energy player1score;
    private String player1name;
    //player2
    private Energy player2score;
    private String player2name;

    /**
     * Creates a new GameOverInterface.
     * @param preloadedExplosion        The explosion animation.
     * @param powerGenerated            The power generated in the previous game.
     * @param name                      The name of the player.
     */
    public GameOverInterface(Animation preloadedExplosion, Energy powerGenerated, String name) {
        explosion = preloadedExplosion;
        
        // Make sure that the explosion plays from the start.
        explosion.reset();
        
        // Set the misc. variables.
        this.powerGenerated = powerGenerated;
        this.name = name;
        this.twoPlayer = false;
        
        // Load the button image for the button and make the button.
        BufferedImage buttonImage = ImageUtils.loadImage("button.png");
        playAgainButton = new PlantGUIElement(buttonImage, 580, 550, 1.2f);
        
        // Add the mouse listener which allows this interface to accept mouse input.
        this.addMouseListener(this);
    }
    
     /**
      * Constructor for the intermission in 2player mode. 
      * @param preloadedExplosion        The explosion animation.
      * @param powerGenerated            The power generated by the first player.
      * @param name                      The name of the first player.
      */   
      public GameOverInterface(Animation preloadedExplosion, Energy powerGenerated, String name, boolean afterFirst) {
             
        explosion = preloadedExplosion;
        
        // Make sure that the explosion plays from the start.
        explosion.reset();
        
        // Set the misc. variables.
        player1score = powerGenerated;
        player1name = name;
        
        this.afterFirst = afterFirst;
        this.twoPlayer = true;
        
        BufferedImage buttonImage = ImageUtils.loadImage("button.png");
        playAgainButton = new PlantGUIElement(buttonImage, 580, 550, 1.2f);
        
        // Add the mouse listener which allows this interface to accept mouse input.
        this.addMouseListener(this);
         }
      
      public GameOverInterface(Animation preloadedExplosion, Energy onePPower, String onePName, Energy twoPPower, String twoPName){
        explosion = preloadedExplosion;
        
        // Make sure that the explosion plays from the start.
        explosion.reset();
        
        //set the scores and names.
        player1score = onePPower;
        player1name = onePName;
        player2score = twoPPower;
        player2name = twoPName;
        
        this.afterFirst = false;
        this.twoPlayer = true;
        
        BufferedImage buttonImage = ImageUtils.loadImage("button.png");
        playAgainButton = new PlantGUIElement(buttonImage, 580, 550, 1.2f);
        
        // Add the mouse listener which allows this interface to accept mouse input.
        this.addMouseListener(this);
      }
      
    @Override
    public void paintComponent(Graphics g) {
        
        // Draw the explosion.
        g.drawImage(explosion.stepImage(), 0, 0, null);
        
     
        // If the explosion is over, show the text and the buttons
        if(explosion.isAtEnd()) {
        
            // Allow text anti-alialising for prettier text and set the font.
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g.setColor(Color.WHITE);
            g.setFont(new Font("Impact", Font.BOLD, 40));
            
               
            if(!twoPlayer){
                // Draw the button.
                g.setColor(Color.BLACK);
                g.drawImage(playAgainButton.getImage(), playAgainButton.x(), playAgainButton.y(), null);
                g.drawString("Play Again!", playAgainButton.x() + 20, playAgainButton.y() + 60);
            
                // Draw the text.
                g.drawString("Game over!", 550, 200);
                g.drawString("Oh no! " + name + ", you blew up the Reactor!", 360, 300);
                g.drawString("However, you generated: " + powerGenerated.toString() + " power before you did!", 210, 400);
                g.drawString("Click to start a new game!", 450, 450);
                }
            
            else if(twoPlayer && afterFirst){        
                // Draw the button.
                g.setColor(Color.BLACK);
                g.drawImage(playAgainButton.getImage(), playAgainButton.x(), playAgainButton.y(), null);
                g.drawString("Round 2!", playAgainButton.x() + 40, playAgainButton.y() + 60);

                // Draw the text.
                g.drawString("End of round.", 550, 200);
                g.drawString("Oh no! " + player1name + ", you blew up the Reactor!", 360, 300);
                g.drawString("However, you generated: " + player1score.toString() + " power before you did!", 210, 400);
                g.drawString("Swap controls, and then click the button below to start round 2.", 130, 450);
                g.drawString("(Loading will take about 10 seconds)", 385,500);
            
            
            }else if(twoPlayer && !afterFirst){
                //Draw the text.
                //needs doing.
                g.drawString("End of round 2.", 550, 100);
                g.drawString("Oh no! " + player2name + ", you blew up the Reactor!", 360, 170);
                g.drawString("" + player2name + "  generated: " + player2score.toString() + " power before they did!", 210, 240);
                g.drawString("" + player1name + " generated: " + player1score.toString() + " on their turn.", 360, 310);
                
                //different text is drawn depending on who won.
                if(player1score.inJoules() > player2score.inJoules()){
                    g.drawString("" + player1name + " WINS!!!", 540, 380);                  
                }  
                }else if(player2score.inJoules() > player1score.inJoules()){
                    g.drawString("" + player2name + " WINS!!!", 540, 380);                    
                }else if(player1score.inJoules() == player2score.inJoules()){
                    g.drawString("Wow, you actually managed to get the same score as each other, impressive.", 210, 380);
                    g.drawString("Unfortunately, this means you both lose. ( ͡° ͜ʖ ͡°) ", 360, 450);
               
                }
            
                g.setColor(Color.BLACK);
                g.drawImage(playAgainButton.getImage(), playAgainButton.x(), playAgainButton.y(), null);
                g.drawString("Play Again!", playAgainButton.x() + 20, playAgainButton.y() + 60);
            
        }
        
        
    }

    /**
     * Called when the mouse is clicked on the screen.
     * @param click         The click event, containing the location.
     */
    @Override
    public void mouseClicked(MouseEvent click) {
        if( clicked(playAgainButton, click) ) {
            block = false;
        }
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }
}
